﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Customs.Components.KDT.SXXK.Info;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.KDT.SXXK
{
	public class DinhMucDangKy : DinhMucDangKyInfo
	{
		public void Load(DinhMucDangKyInfo entityInfo)
		{
			this._SoTiepNhan = entityInfo.SoTiepNhan;
			this._MaHaiQuan = entityInfo.MaHaiQuan;
			this._NamTiepNhan = entityInfo.NamTiepNhan;
			this._MaSP = entityInfo.MaSP;
			this._MaDoanhNghiep = entityInfo.MaDoanhNghiep;
			this._SoDinhMuc = entityInfo.SoDinhMuc;
			this._NgayDangKy = entityInfo.NgayDangKy;
			this._NgayApDung = entityInfo.NgayApDung;			
			this._TrangThaiXuLy = entityInfo.TrangThaiXuLy;					    
            this._DMInfoCollection = entityInfo.DMInfoCollection;

		}
		
		//---------------------------------------------------------------------------------------------

		public void Insert(SqlTransaction transaction)
		{
			StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DDINHMUCDK_SP ( ");
			query.Append("So_TNDKDT, ");
			query.Append("MA_HQTN, ");
			query.Append("Nam_TN, ");
			query.Append("Ma_SP, ");
			query.Append("MA_DV, ");
			query.Append("SO_DM, ");
			query.Append("NGAY_DK, ");
			query.Append("NGAY_AD, ");			
			query.Append("TrangThaiXL ");			
			query.Append(") ");
			query.Append("VALUES (");
			query.Append("@So_TNDKDT, ");
			query.Append("@MA_HQTN, ");
			query.Append("@Nam_TN, ");
            query.Append("@Ma_SP, ");
			query.Append("@MA_DV, ");
			query.Append("@SO_DM, ");
			query.Append("@NGAY_DK, ");
			query.Append("@NGAY_AD, ");			
			query.Append("@TrangThaiXL ) ");			
			
			SqlParameter[] parameters = new SqlParameter[9];
			
			parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
			parameters[0].Direction = ParameterDirection.Input;
			parameters[0].Value = this._SoTiepNhan;
			
			parameters[1] = new SqlParameter("@MA_HQTN", SqlDbType.Char);
			parameters[1].Size = 6;
			parameters[1].Direction = ParameterDirection.Input;
			parameters[1].Value = this._MaHaiQuan;
			
			parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
			parameters[2].Direction = ParameterDirection.Input;
			parameters[2].Value = this._NamTiepNhan;

            parameters[3] = new SqlParameter("@Ma_SP", SqlDbType.VarChar);
			parameters[3].Direction = ParameterDirection.Input;
			parameters[3].Value = this._MaSP;
			
			parameters[4] = new SqlParameter("@MA_DV", SqlDbType.VarChar);
			parameters[4].Size = 14;
			parameters[4].Direction = ParameterDirection.Input;
			parameters[4].Value = this._MaDoanhNghiep;
			
			parameters[5] = new SqlParameter("@SO_DM", SqlDbType.Int);
			parameters[5].Direction = ParameterDirection.Input;
			parameters[5].Value = this._SoDinhMuc;
			
			parameters[6] = new SqlParameter("@NGAY_DK", SqlDbType.DateTime);
			parameters[6].Direction = ParameterDirection.Input;
			parameters[6].Value = this._NgayDangKy;
			
			parameters[7] = new SqlParameter("@NGAY_AD", SqlDbType.DateTime);
			parameters[7].Direction = ParameterDirection.Input;
			parameters[7].Value = this._NgayApDung;	
			
			parameters[8] = new SqlParameter("@TrangThaiXL", SqlDbType.Int);
			parameters[8].Direction = ParameterDirection.Input;
			parameters[8].Value = this._TrangThaiXuLy;
						
			
			SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);			
		}
		
		//-----------------------------------------------------------------------------------------
		
		
		public bool Load()
		{
			SqlDataReader reader = this.Select();
			if (reader.Read())
			{
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) this._SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) this._MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamTiepNhan"))) this._NamTiepNhan = reader.GetInt16(reader.GetOrdinal("NamTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MASP"))) this._MaSP = reader.GetString(reader.GetOrdinal("MASP"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) this._MaDoanhNghiep = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoDinhMuc"))) this._SoDinhMuc = reader.GetInt32(reader.GetOrdinal("SoDinhMuc"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayDangKy"))) this._NgayDangKy = reader.GetDateTime(reader.GetOrdinal("NgayDangKy"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayApDung"))) this._NgayApDung = reader.GetDateTime(reader.GetOrdinal("NgayApDung"));				
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) this._TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));				
                reader.Close();
				return true;
			}
            reader.Close();
			return false;
		}		
		
		//---------------------------------------------------------------------------------------------
		
		/// <summary>
		/// Tìm thông tin đăng ký định mức theo [SoTiepNhan, MaHaiQuan, NamTiepNhan].
		/// </summary>
		/// <returns>Thông tin đăng ký định mức.</returns>
		public SqlDataReader Select()
		{
			string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
			StringBuilder query = new StringBuilder();
			query.Append("SELECT ");
			query.Append("So_TNDKDT AS [SoTiepNhan], ");
			query.Append("Ma_HQTN AS [MaHaiQuan], ");
			query.Append("Nam_TN AS [NamTiepNhan], ");
            query.Append("MA_SP AS [MASP], ");
			query.Append("Ma_DV AS [MaDoanhNghiep], ");
			query.Append("So_DM AS [SoDinhMuc], ");
			query.Append("Ngay_DK AS [NgayDangKy], ");
			query.Append("Ngay_AD AS [NgayApDung], ");			
			query.Append("TrangThaiXL AS [TrangThaiXuLy] ");			
            query.Append("FROM dbo.[DDINHMUCDK_SP] ");
			query.Append("WHERE ");
			query.Append("So_TNDKDT = @SoTiepNhan AND Ma_HQTN = @MaHaiQuan AND Nam_TN = @NamTiepNhan");
			
			SqlParameter[] parameters = new SqlParameter[3];
			parameters[0] = new SqlParameter("@SoTiepNhan", SqlDbType.BigInt);
			parameters[0].Value = this._SoTiepNhan;

			parameters[1] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
			parameters[1].Value = this._MaHaiQuan;

			parameters[2] = new SqlParameter("@NamTiepNhan", SqlDbType.SmallInt);
			parameters[2].Value = this._NamTiepNhan;

			return SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString(), parameters);				
		}

         //-----------------------------------------------------------------------------------------
        public static void Delete(SqlTransaction transaction, long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan)
        {
            string query = string.Format("DELETE FROM DDINHMUCDK_SP WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3} ", soTiepNhan, maHaiQuan.PadRight(6), maDoanhNghiep, namTiepNhan);
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
        }

	}
}
